Negative Numbers

This works for all positive numbers but how do we show negative numbers?

1001

Sign and Magnitude

The simpler method is to reserve the leftmost bit to indicate the sign: 0 for positive and 1 for negative.

0010 = 2

1010 = -2

1111 = -7

One's Complement

This method involves creating the inverse of the binary representation after it has been converted.

Example

Represent the decimal value -14 in binary using One's Complement:

Step 1 - Convert 14 to binary

1110

Step 2 - Flip all the bits (only do this if the number is negative)

0001

Exercise

Using sign and magnitude, what are the decimal representations of these numbers?

  • 0010
  • 1110
  • 1100
  • 10001

Using one's complement, convert these values between decimal or binary, based on their original format:

  • 8
  • -18
  • 4
  • 1011
  • 2
  • -6
  • -4
  • -1
  • 1000
  • 10010 = binary
    • 101101 = one's complement
  • 0100
  • -4

2's Complement

Two's complement is designed to make arithmetic with binary much easier, and is another way of storing signed binary numbers. The conversion process builds on from one's complement.

The negative number will have a one at the leftmost bit, so be sure to add bits to allow this

Example: -3 in 2's complement

Step 1: Convert to decimal (stop here if the number is positive)

0011

Step 3: Perform one's complement

1100

Step 4: Add 1 to the result and show the binary representation:

1101 is -3 in 2's complement

Exercise

Show the two's complement representation of these numbers:

  • 3
  • -10
  • -8

Convert these two's complement numbers back to their decimal representation:

  • 1001
  • 0011
  • 11011
  • 0011
  • 01010
    • 10101 - one's
    • 10110 - two's
  • 01000

    • 10111 - one's
    • 11000 - two's
  • -7

  • 3
  • -5

Conversion

Binary Arithmetic

Binary arithmetic is much like standard arithmetic and you will probably recognise the methods from how you performed arithmetic in school. The below example is addition of unsigned binary numbers:

Decimal Binary
4 0100
+ 2 0010
--- ---
6 0110

Binary Arithmetic

In this example, we see that we need to carry the bit across.

So, where 1 appears in both columns, the result becomes 0 and the 1 is carried over to the next column to the left

Decimal Binary
4 0100
+ 6 0110
--- --- (carry)
10 1010

Subtraction

Or: Adding Negative Numbers

How do I calculate 32 - 12 in binary?

Sure, I could convert to decimal and then convert back, but this is less than ideal.

This is where Two's Complement is vital.

32 - 12 can become 32 + (-12)

Step 1: 32 in binary

0010 0000

Step 2: 12 in binary

0000 1100

Step 3: Two's complement of 12

1111 0011

Add one:

1111 0100

Step 4: Subtract

Decimal Binary
32 0010 0000
+ -12 1111 0100
--- --- (carry)
20 001 0100

Another Way

Decimal Binary
13 0000 1101
-6 0000 0110
--- ---
20 0000 0111
0000 00B0

B - Borrow

Animated example

Exercise

Do exercise sheet on LC